package com.cy.springboot;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletRegistration;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author ChenYan
 * @Date 2021/8/10 11:05
 * @Description
 */
public class SpringApplication {

    private Set<Class<?>> primarySources;

    private Tomcat tomcat;

    private int port = 8080;

    public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";

    SpringApplication(Class<?>... primarySource){
        Assert.notNull(primarySource, "primarySource不能为空");
        //将传入的primarySource传入primarySources中
        this.primarySources = new LinkedHashSet<Class<?>>(Arrays.asList(primarySource));
        //1.1 推断应用类型
        //1.2 初始化spring.factories中已配置的ApplicationContextInitializer
        //1.3 初始化classpath下所有配置的ApplicationListener
        //1.4 推算出main方法的类名
    }

    public static void run(Class<?> primarySource, String[] args) {
        new SpringApplication(primarySource).run(args);
    }

    private void run(String... args) {
        ConfigurableApplicationContext context = null;

        //1.获取启动监听器
        //2.构造应用上下文环境，读取相关配置文件
        //3.创建应用上下文
        context = createApplicationContext();
        //4.准备上下文
        //5.刷新上下文
        refreshContext(context);
        //6.最后处理
    }

    private void refreshContext(ConfigurableApplicationContext context) {
        //此处仅作创建tomcat用
        refresh(context);
    }

    private void refresh(ConfigurableApplicationContext context) {
        createWebServer();
    }

    /**
     * 创建webServer
     */
    private void createWebServer() {

        try {
            Tomcat tomcat = new Tomcat();
            this.tomcat = tomcat;
            File baseDir = File.createTempFile("tomcat",".");
            tomcat.setBaseDir(baseDir.getAbsolutePath());
            tomcat.addWebapp("/", "D:\\");
            tomcat.setPort(port);
            //关闭热部署
            tomcat.getHost().setAutoDeploy(false);
            tomcat.start();
            System.out.println("Tomcat started on port: " + port);
            tomcat.getServer().await();
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

    private void customizeConnector(Connector connector) {

        connector.setPort(port);

        // Don't bind to the socket prematurely if ApplicationContext is slow to start
        connector.setProperty("bindOnInit", "false");

    }

    /**
     * 创建上下文
     * @return
     */
    private ConfigurableApplicationContext createApplicationContext() {
        return null;
    }
}
